	import model.UserModel;
	
	import mx.controls.Alert;
	import mx.rpc.xml.SimpleXMLEncoder;
	
	import nrb.utils.DateHandler;
	import nrb.utils.XMLHelper;
	
	
	private var helper_xml:XMLHelper;
	private var xml_users:XML;
	private var user_model:UserModel;
	private var dates:DateHandler;
	private var pA:Object = new Object();
	
	public function func_init(): void {
		helper_xml = new XMLHelper();
		xml_users = new XML();
		user_model = new UserModel();
		dates = new DateHandler();
		pA = new Object();
		
		pA = parentApplication;
		//check xml file if exist, else change login button to create user button
		if (!helper_xml.func_check_fileexists(pA.FILENAME_USERS_XML)){
			btn_login.label = 'Create User';
		}else{
			btn_login.label = 'Login';
		}
		
		txt_userid.setFocus();
		
	}
	
	
	public function func_login(): void {
		//do login here
		if(func_check_fields()){
			if (!helper_xml.func_check_fileexists(pA.FILENAME_USERS_XML)){
				func_new();
				//set session
				set_session(txt_userid.text, txt_password.text, 'Admin');
				parentApplication.func_change_state(pA.STATE_MAIN);
			}else{
				//open file
				helper_xml.func_open_xml(pA.FILENAME_USERS_XML);
				xml_users = helper_xml.func_get_xml();
		

				//check if record exists
				if(xml_users.user.(password == txt_password.text && userid == txt_userid.text).userid != undefined){
					//set session
					set_session(txt_userid.text, txt_password.text, xml_users.user.(password == txt_password.text && userid == txt_userid.text).userrole);
					parentApplication.func_change_state(pA.STATE_MAIN);
				}else{
					Alert.show('No Record Found');
				}
			}
		}else{
			Alert.show('Fields Required');
		}
		
		
	}
	
	
	public function func_check_fields(): Boolean {
		if(txt_password.text == '' || txt_userid.text == ''){
			txt_userid.setFocus();
			txt_password.setFocus();
			return false;
		}
		return true;
	}
	
	//creates user if xml file not found
	private function func_new(): void {
		var xml_parentnode:XML = XML("<users></users>");
		helper_xml.func_create_xml(xml_parentnode, pA.FILENAME_USERS_XML);
		
		//set user attributes
		user_model = new UserModel();
		user_model.userid = txt_userid.text;
		user_model.password = txt_password.text;
		user_model.userrole = 'Admin';
		user_model.datecreated = dates.get_full_date('YYYY-MM-DD HH:NN:SS');
		user_model.datemodified = dates.get_full_date('YYYY-MM-DD HH:NN:SS');
		user_model.createdby = txt_userid.text;
		user_model.modifiedby = txt_userid.text;
		
		helper_xml.func_open_xml(pA.FILENAME_USERS_XML);
		xml_users = helper_xml.func_get_xml();
		
		//object to user xml node
		var q_name:QName = new QName('user');
		var xml_doc:XMLDocument = new XMLDocument();
		var xml_encoder:SimpleXMLEncoder = new SimpleXMLEncoder(xml_doc);
		var xml_node:XMLNode = xml_encoder.encodeValue(user_model,q_name,xml_doc);
		var user_xml_node:XML = new XML(xml_doc.toString());		
		xml_users.appendChild(user_xml_node);
		helper_xml.func_write_xml(xml_users);
	}
	
	private function set_session(userid:String, password:String, userrole:String): void {
		pA.LOGGED_USER_ID = userid;
		pA.LOGGED_PASSWORD = password;
		
		pA.IS_ADMIN = userrole == 'Admin';
	}
